package com.example.warehouse.bus.controller;

import com.example.warehouse.bus.pojo.entity.BusOutport;
import com.example.warehouse.bus.pojo.param.BusOutportAddNewParam;
import com.example.warehouse.bus.pojo.param.BusOutportUpdateParam;
import com.example.warehouse.bus.pojo.vo.BusInportListItemVO;
import com.example.warehouse.bus.pojo.vo.BusOutportListItemVO;
import com.example.warehouse.bus.service.IBusOutportService;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.security.CurrentPrincipal;
import com.example.warehouse.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Currency;


/**
 * 处理退货数据的控制类
 * @author XXH
 */
@Slf4j
@RestController
@RequestMapping("/outport")
@Api(tags = "0.2 退货管理")
@Validated
public class BusOutportController {

    @Autowired
    IBusOutportService outportService;

    public BusOutportController() {
        log.info("创建控制器对象:BusOutportController");
    }

    @ApiOperation("添加退货数据")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/bus/outport/add-new')")
    @PostMapping("/insert")
    public JsonResult insert(BusOutportAddNewParam busOutportAddNewParam){
        log.debug("添加退货,Controller,参数:退货实体类{}:",busOutportAddNewParam);
        outportService.insert(busOutportAddNewParam);
        return JsonResult.ok();
    }

    @ApiOperation("删除退货数据")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/bus/outport/delete')")
    @PostMapping("/delete")
    public JsonResult deleteById(Long id,@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        log.debug("逻辑删除退货,Controller,参数:退货实体类{}:",currentPrincipal);
        outportService.deleteById(id,currentPrincipal);
        return JsonResult.ok();
    }

    //TODO 修改数据时如果enable值为0时没有给回传信息,需要在业务逻辑当中进行修改;
    @ApiOperation("修改退货数据")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/bus/outport/update')")
    @PostMapping("/update")
    public JsonResult updateById(Long id, BusOutportUpdateParam busOutportUpdateParam){
        log.debug("逻辑删除退货,Controller,参数:退货实体类{}:",busOutportUpdateParam);
        outportService.updateById(id,busOutportUpdateParam);
        return JsonResult.ok();
    }

    /**
     * 查询列表的第二个方法,用户主动传入
     * @param page
     * @return
     */
    @ApiOperation("查询退货列表")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/bus/outport/select')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据数目,不填默认为5页", dataType = "int")

    })
    @GetMapping("/select")
    public JsonResult list(Integer page,Integer pageSize) {
        log.debug("开始处理【查询进货列表】请求，页码：{},每页条目数{}", page,pageSize);
        Integer pageNum = page == null ? 1 : page;
        PageData<BusOutportListItemVO> pageData;
        if (pageSize==null){
            pageData = outportService.list(pageNum);
        }else{
            pageData = outportService.list(pageNum,pageSize);
        }
        return JsonResult.ok(pageData);
    }




}
